let imgPath = null;
let pwd = null;
let status = false;
// 判断两次输入密码是否一致
$('#checkpwd').blur(function () {
    let onetime = $('#inppwd').val()
    let sectime = $('#checkpwd').val()
    if (onetime != sectime) {
        $('#checkpwd').attr('type', 'text')
        $('#checkpwd').css('color', 'red')
        $('#checkpwd').val('密码不一致,请重新输入')
        status = true;
        setTimeout(() => {
            $('#checkpwd').attr('type', 'password')
            $('#checkpwd').val('')
        }, 2000)
    } else {
        status = false;
    }
})
// 上传图片
$('#inpimg').change(function () {
    let fd = new FormData();
    fd.append('image', $('#inpimg')[0].files[0]);
    $.ajax({
        method: 'post',
        url: 'http://localhost:3000/upload/image',
        data: fd,
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data)
            imgPath = data.path;
            $('#inpimg').css('opacity', 0)
            $('.img').attr('src', data.path);
        },
        error: function (e) {
            console.log(JSON.stringify(e));
        }
    })
})
$('#registerForm').submit((e) => {
    e.preventDefault();
    if (!status) {
        $.ajax({
            method: 'post',
            url: 'http://localhost:3000/user/register',
            data: {
                "username": $('#inpname').val(),
                "password": $('#inppwd').val(),
                "phone": $('#inpnum').val(),
                "img": imgPath
            },
            success: function (data) {
                console.log(data)
                if (data.code == 0) {
                    alert('注册成功')
                } else {
                    alert('注册失败:' + data.msg)
                }
            },
            error: function (data) {
                console.log('注册失败' + JSON.stringify(e))
            }
        })
    }
})
